package org.example.operator;

import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.example.bean.WaterSensor;

public class 聚合算子ReduceOperator {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);


        DataStreamSource<WaterSensor> source = env.fromElements(
                new WaterSensor("s1", 1L, 1),
                new WaterSensor("s2", 1L, 2),
                new WaterSensor("s1", 1L, 13));

        /**
         *
         */
        KeyedStream<WaterSensor, String> keyBy = source.keyBy(value -> {
            return value.getType();
        });

        // 每个key的第一条数据来的时候，不会执行reduce 方法，存起来，直接输出
        keyBy.reduce((value1, value2) -> {
            return new WaterSensor(value1.getType(), value2.getSensorRecordTimestamp(), 20);
        }).print();


        env.execute();
    }
}

